package com.wanxi.education.service.impl;

import com.wanxi.education.mapper.LoginMapper;
import com.wanxi.education.mapper.UserMapper;
import com.wanxi.education.model.vo.user.*;
import com.wanxi.education.service.LoginService;
import com.wanxi.education.service.UserLoginService;
import com.wanxi.education.service.redis.RedisService;
import com.wanxi.education.util.DrawCheck;
import com.wanxi.education.util.JwtTokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @Author: YanZhengYi
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    /**
     * 超管/管理员账号登录
     *
     * @param loginVo
     * @return
     */
    @Override
    public Map<String, String> adminLogin(LoginVo loginVo) {
        Map<String, String> data = userLoginService.LoginPass(loginVo);
        return data;
    }

    /**
     * @param phone 登录手机号码
     * @return
     */
    @Override
    public UserVO findByPhone(String phone) {
//        -1 忽略该字段，只需要传递一个值做充数即可
        UserVO userVO = loginMapper.findByPhone(phone,-1);
        return userVO;
    }

    /**
     * @param phone 登录手机号码
     * @param state 0:管理员 1：机构负责人 2：学员
     * @return
     */
    @Override
    public UserVO findByPhone(String phone, Integer state) {
        UserVO userVO = loginMapper.findByPhone(phone, state);
        return userVO;
    }

    @Override
    public Map<String, String> BLoginByPass(BUserLoginVo bUserLoginVo) {
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(bUserLoginVo,loginVo);
           Map<String,String> result = userLoginService.bUserAccountLoginPass(loginVo);
        return result;
    }

    @Override
    public Map<String, String> BLoginByCode(BUserPhoneLoginVo bUserLoginVo) {
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(bUserLoginVo,loginVo);
        Map<String, String> result = userLoginService.bUserAccountLoginCode(loginVo);
        return result;
    }

    @Override
    public Map<String, String> CLoginByCode(CUserPhoneLoginVo cUserLoginVo) {
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(cUserLoginVo,loginVo);
        Map<String, String> result = userLoginService.cUserAccountLoginCode(loginVo);
        return result;
    }

    @Override
    public String changePassword(CUserChangePasswordVo cUserChangePasswordVo) {
        cUserChangePasswordVo.setPassword(passwordEncoder.encode(cUserChangePasswordVo.getPassword()));
        Integer result = loginMapper.changePasswordByPhone(cUserChangePasswordVo.getPhone(),cUserChangePasswordVo.getPassword());
        return result==1?"success":"false";
    }

    @Override
    public Map<String, String> CLoginByPass(CUserLoginVo cUserLoginVo) {
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(cUserLoginVo,loginVo);
        Map<String,String> result = userLoginService.cUserAccountLoginPass(loginVo);
        return result;
    }


    public Map<String,String> getPictureCode() {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        DrawCheck drawCheck = new DrawCheck();

        //1.在内存中创建一张图片
        BufferedImage bi = new BufferedImage(120, 30,BufferedImage.TYPE_INT_RGB);
        //2.得到图片
        Graphics g = bi.getGraphics();
        //3.设置图片的背影色
        drawCheck.setBackGround(g);
        //4.设置图片的边框
        drawCheck.setBorder(g);
        //5.在图片上画干扰线
        drawCheck.drawRandomLine(g);
        //6.写在图片上随机数
        String random =  drawCheck.drawRandomNum((Graphics2D) g);//生成中文验证码图片

        //7.将随机数存在redis中
        Random randomNum = new Random(new Date().getTime());
        int length = randomNum.nextInt(3)+3;
        String num = Long.toString(System.currentTimeMillis()).substring(length);
        //放置在redis里
        redisService.set("codePicId-"+num,random,300);

        //10.将图片写给浏览器
        try {
            ImageIO.write(bi, "jpg", output);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] captcha = output.toByteArray();
        //
//        BASE64Encoder encoder = new BASE64Encoder();
        String imageStr = Base64.getEncoder().encodeToString(captcha);
//        String imagestr =  encoder.encode(captcha);
        HashMap<String, String> map = new HashMap<>();
        map.put("codePic",imageStr);
        map.put("pic_id",num);
        return map;
    }


    public void getPhoneCode(String phone){
        //生成随机的数字
        int code = (int)(Math.random() * 10000);
        //放置在redis里
        redisService.set("codeTel-"+phone,1234+"",300);
//        smsToPhone.createClient(phone,code);
    }

}
